package club.yuit.desktop.ui;

import club.yuit.desktop.GlobalData;
import club.yuit.desktop.layout.InLineLayout;
import club.yuit.rmq.MQAdminFactory;
import club.yuit.rmq.config.RMQConfigure;
import org.apache.rocketmq.client.exception.MQBrokerException;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.remoting.exception.RemotingConnectException;
import org.apache.rocketmq.remoting.exception.RemotingSendRequestException;
import org.apache.rocketmq.remoting.exception.RemotingTimeoutException;
import org.apache.rocketmq.remoting.protocol.body.ClusterInfo;
import org.apache.rocketmq.tools.admin.MQAdminExt;

import javax.swing.*;
import java.awt.*;
import java.net.URL;

/**
 * @author: yuit
 * @date: 2023/11/27 17:44
 */
public class ConfigUI extends JFrame {

    private MainUI mainUI;
    private MQAdminExt admin;


    public ConfigUI() {
        this.setTitle("配置链接");
        this.setSize(600, 200);
        this.setContentPane(container());
        this.setVisible(true);
        this.setLocationRelativeTo(null);
        URL resource = getClass().getResource("/imgs/Icon.png");
        ImageIcon icon = new ImageIcon(resource);
        this.setIconImage(icon.getImage());
        this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        this.setResizable(false);
        this.admin = admin;
    }


    public void initRMQAdmin(String addr, JTextField attrFiled) {
        MQAdminFactory factory = new MQAdminFactory();
        RMQConfigure c = new RMQConfigure();
        c.setNamesrvAddr(addr);
        String errorMsg = "";
        try {
            this.admin = factory.getInstance(c);
            ClusterInfo clusterInfo = null;
            clusterInfo = admin.examineBrokerClusterInfo();
            GlobalData.MQ_DATA.put(GlobalData.MQ_CLUSTER_INF, clusterInfo);
        } catch (MQClientException | InterruptedException | MQBrokerException | RemotingTimeoutException |
                 RemotingSendRequestException e) {
            errorMsg = e.getMessage();
            admin.shutdown();
        } catch (RemotingConnectException e) {
            errorMsg="链接错误，请确认地址是否正确！";
            admin.shutdown();
        }

        if (!errorMsg.isEmpty()){
            JOptionPane.showMessageDialog(ConfigUI.this, errorMsg,"提示",JOptionPane.ERROR_MESSAGE);
        }
    }


    private Container container() {
        JPanel panel = new JPanel();
        InLineLayout layout = new InLineLayout(5, InLineLayout.ALIGN_CENTER);
        panel.setLayout(layout);

        JTextField address = new JTextField();
        address.setSize(250, 0);
        address.putClientProperty("JTextField.placeholderText", "name server 地址（127.0.0.1:9876）");
        panel.add(address);


        JButton nc = new JButton("链接");
        nc.addActionListener(e -> {
            try {
                String addr = address.getText();
                nc.setEnabled(false);
                if (addr == null || addr.isEmpty()) {
                    JOptionPane.showMessageDialog(ConfigUI.this, "服务地址不能为空");
                    nc.setEnabled(true);
                } else {
                    initRMQAdmin(addr, address);

                    mainUI = new MainUI();
                    mainUI.setVisible(true);
                    // 关闭当前窗口及其资源
                    dispose();
                }
            } catch (Exception ex) {
                nc.setEnabled(true);
            }

        });
        panel.add(nc);

        return panel;
    }


}
